package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController("userOrderController")
@RequestMapping(value = "/user/order")
@Api(tags ="订单相关接口")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    //用户下单
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody Orders orders){
        OrderSubmitVO orderSubmitVO =orderService.submit(orders);
        return Result.success(orderSubmitVO);
    }
    @PutMapping("/payment")
    @ApiOperation("微信支付")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws IOException {
 //更改状态
        orderService.updatePaymentSuccessfulMsg(ordersPaymentDTO);
//         HashMap<String, String> map = new HashMap<>();
//         map.put("orderNumber",ordersPaymentDTO.getOrderNumber());
//         map.put("payMethod",ordersPaymentDTO.getPayMethod().toString());
//         HttpClientUtil.doPost("http://localhost:8080/user/order/paymentSuccessful",map);



        return Result.success();
    }
    @PostMapping("/paymentSuccessful")
    @ApiOperation("支付成功")
    public Result paymentSuccessful(@RequestBody OrdersPaymentDTO ordersPaymentDTO){
        //更改状态
        orderService.updatePaymentSuccessfulMsg(ordersPaymentDTO);

        return Result.success();
    }
     @GetMapping("/orderDetail/{id}")
    @ApiOperation("根据id查询订单信息")
    public Result<OrdersDTO> orderDetail(@PathVariable Long id){
         OrdersDTO order = orderService.orderDetailById(id);

        return Result.success(order);
    }
    @GetMapping("/historyOrders")
    @ApiOperation("分页查询用户历史订单")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO){
        ordersPageQueryDTO.setUserId(BaseContext.getCurrentId());
        PageResult pageResult = orderService.pageQuery(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

}
